package com.nathan.crm.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nathan.crm.dao.CustomerMapper;
import com.nathan.crm.query.CustomerQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReportService {

    @Resource
    private CustomerMapper customerMapper;

    /**
     * 查询客户贡献
     * @param customerQuery
     * @return
     */
    public Map<String,Object> queryCustomerContribution(CustomerQuery customerQuery){
        Map<String,Object> result = new HashMap<>();
        PageHelper.startPage(customerQuery.getPage(),customerQuery.getLimit());
        PageInfo<Map<String,Object>> pageInfo =new PageInfo<>(customerMapper.queryCustomerContribution(customerQuery));
        result.put("count",pageInfo.getTotal());
        result.put("data",pageInfo.getList());
        result.put("code",0);
        result.put("msg","");
        return result;
    }

    /**
     * 柱状图：查询客户构成（各个级别的客户的数量）
     * @return
     */
    public Map<String, Object> countCustomerLevel() {
        // 定义一个map集合，值是两个list集合，list集合中存放数据
        Map<String,Object> map = new HashMap<>();
        // 柱状图x轴数据（客户级别）
        List<String> xData = new ArrayList<>();
        // 柱状图y轴数据（各个级别的客户的数量）
        List<Integer> yData = new ArrayList<>();

        // 获取客户构成数据
        List<Map<String, Object>> customerMakeList = customerMapper.countCustomerLevel();

        if (null != customerMakeList && customerMakeList.size() > 0){
            // 拿到里面的每一个map集合
            customerMakeList.forEach(levelTotal -> {
                String level = levelTotal.get("name").toString();
                Integer total = Integer.parseInt(levelTotal.get("value").toString());

                xData.add(level);
                yData.add(total);
            });
        }

        map.put("levelData",xData);
        map.put("totalData",yData);

        return map;
    }

    /**
     * 饼状图：查询客户构成
     * @return
     */
    public List<Map<String, Object>> countCustomerLevel2() {
        return customerMapper.countCustomerLevel();
    }
}
